<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 配送员评论模型
 * @author www.dingwei.cn
 */

class DlnCommentModel extends MY_Model{
	
	public $tbComment = 'deliveryman_comment';
	public $tbUser = 'deliveryman';
	
	function __construct(){
		parent::__construct();
	}

    /**
     * 保存评论
     * @param array $data 数据
     * @param int   $id 评论id
     * @return boolean|boolean|number
     * @author zyl
     */
    public function save($data,$id = 0){
        if ($id){
            $this->db->where('id',$id);
            $query = $this->update($this->tbComment, $data);
            return $query === false ? false : true;
        }else{
            return $this->insert($this->tbComment, $data);
        }
    }

    /**
     * 统计商户的平均分
     * @param int       $deliverymanId
     * @return float
     */
    public function totalCommentScore($deliverymanId){
        $this->db
            ->select('avg(score) as avg_score')
            ->where('deliveryman_id',$deliverymanId);
        $tA = $this->getOne($this->tbComment);
        return round(empty($tA['avg_score'])?5.0:$tA['avg_score'],2);
    }

    /**
     * 后台获取评论数据
     * @param array $ci
     * @return array
     */
    public function adminGetList($ci = array()){
        $nickname = trim(get('m_name'));
        $deliveryman = trim(get('deliveryman')); // 配送员名称
        $score = trim(get('score'));
        $sendDateFrom = strtotime(trim(get('send_date_from')));
        $sendDateTo = strtotime(trim(get('send_date_to')));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

//        if(!empty($ci)){
//            $this->db->where_in('s.county',$ci);
//        }
        if ($nickname){
            $this->db->like('s.shop_name',$nickname);
        }
        if ($deliveryman){
            $this->db->like('u.name',$deliveryman);
        }
        if ($score){
            $this->db->like('co.score',$score);
        }
        if($sendDateFrom){
            $this->db->where('co.addtime >=', $sendDateFrom);
        }
        if($sendDateTo){
            $this->db->where('co.addtime <', strtotime('+1day', $sendDateTo));
        }

        if ($iSortCol_0 == 3){
            $this->db->order_by('co.score', $sSortDir_0);
        }else if ($iSortCol_0 == 4){
            $this->db->order_by('co.addtime', $sSortDir_0);
        }else{
            $this->db->order_by('co.addtime desc');
        }
        get_instance()->loadModel('ConsumerOrderModel','mCOrder','consumer');
        get_instance()->loadModel('MerchantUserModel','mMerchant','merchant');
        $this->db->select('co.*,s.shop_name as m_name,u.name as deliveryman')
            ->join($this->tbUser.' u', 'co.deliveryman_id=u.id')
            ->join($this->mCOrder->tbOrder.' o', 'co.order_id=o.id')
            ->join($this->mMerchant->tbUser.' m','o.merchant_id = m.id','left')// 商家
            ->join($this->mMerchant->tbShop.' s','s.merchant_id=m.id'); // 店铺
        return $this->pagination($this->tbComment.' co');
    }
}